package com.magnificent.panda.model.generator.log;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * User: Babanin
 * Date: 07.10.2010
 * Time: 15:40:56
 */
public class PatentLogParser {

    private static final int DATE_COLUMN = 5;
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
    private static final String TEMPLATE_EXCEL_DOC = "patents2007.xls";

    private static final Calendar BEGIN_OF_YEAR = Calendar.getInstance();

    public static void main(String[] args) throws BiffException, IOException, ParseException {
        PrintWriter writer = new PrintWriter(new FileOutputStream("output\\patents.txt"));

        Workbook templateWorkbook = Workbook.getWorkbook(new File(TEMPLATE_EXCEL_DOC));
        Sheet templateSheet = templateWorkbook.getSheet(2);

        BEGIN_OF_YEAR.set(Calendar.YEAR, 2007);
        BEGIN_OF_YEAR.set(Calendar.DAY_OF_MONTH, 1);
        BEGIN_OF_YEAR.set(Calendar.MONTH, 1);
        BEGIN_OF_YEAR.set(Calendar.HOUR_OF_DAY, 0);
        BEGIN_OF_YEAR.set(Calendar.MINUTE, 0);
        BEGIN_OF_YEAR.set(Calendar.SECOND, 0);
        BEGIN_OF_YEAR.set(Calendar.MILLISECOND, 0);

        Calendar c = Calendar.getInstance();
        for (int i = 0; i < templateSheet.getRows(); i++) {
            String content = templateSheet.getCell(DATE_COLUMN, i).getContents().trim();

            if (!"".equals(content)) {
                Date date = DATE_FORMAT.parse(content);

                c.setTime(date);
                if (c.get(Calendar.YEAR) == 7) {
                    writer.write("" + c.get(Calendar.DAY_OF_YEAR) + "\n");
                }
            }
        }

        templateWorkbook.close();
        writer.close();
    }
}

  /* converted to utf8 */